﻿using System;
using System.IO;
using System.Web;

namespace ZSoftInstagram.Services.Upload
{
    public class DateDirectoryProvider : IUploadDirectoryProvider
    {
        private string UserName
        {
            get
            {
                if (HttpContext.Current.User == null)
                {
                    throw new InvalidOperationException();
                }

                return HttpContext.Current.User.Identity.Name;
            }
        }

        public string ProvidePath()
        {
            var path = Path.Combine(this.Root,
                                    this.UserName,
                                    DateTime.Now.Year.ToString(),
                                    DateTime.Now.Day.ToString());

            if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
            }

            return path;
        }

        public string Root
        {
            get
            {
                return "/Files";
            }
        }
    }
}
